//
//  HomeCell.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/9/16.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit
import SDWebImage
import HYLabel

fileprivate let edgeMargin : CGFloat = 15
fileprivate let itemMargin : CGFloat = 10

class HomeCell: UITableViewCell {
    
    // MARK:- 控件属性
    @IBOutlet weak var profileImageView: UIImageView!               //用户头像
    @IBOutlet weak var verifiedImgView: UIImageView!                //认证类型
    @IBOutlet weak var nickNameLabel: UILabel!                      //昵称
    @IBOutlet weak var vipImgView: UIImageView!                     //会员等级
    @IBOutlet weak var timeLabel: UILabel!                          //发布时间
    @IBOutlet weak var sourceLabel: UILabel!                        //发布来源
    @IBOutlet weak var contentLabel: HYLabel!                       //发布内容
    @IBOutlet weak var picCollectionView: HomeCollectionView!       //发布配图
    @IBOutlet weak var retweetedLabel: HYLabel!                     //转发微博内容
    @IBOutlet weak var retweetBgView: UIView!
    @IBOutlet weak var toolView: UIView!
    
    // MARK:- 约束属性
    @IBOutlet weak var statusWidth: NSLayoutConstraint!             //微博正文的宽
    @IBOutlet weak var pictureViewH: NSLayoutConstraint!            //配图的高
    @IBOutlet weak var pictureViewW: NSLayoutConstraint!            //配图的宽
    @IBOutlet weak var retweetLabelTopconstant: NSLayoutConstraint!
    @IBOutlet weak var picViewBottomConstant: NSLayoutConstraint!
    
    // MARK:- 自定义的属性
    var viewModel : StatusViewModel? {
        didSet {
            //nil值校验
            guard let viewModel = viewModel else {
                return
            }
            
            profileImageView.layer.cornerRadius = 20.0
            profileImageView.layer.masksToBounds = true
            profileImageView.sd_setImage(with: viewModel.profileURL, placeholderImage: UIImage(named: "avatar_default_small"), options: [], completed: nil)
            verifiedImgView.image = viewModel.verifiedImage
            nickNameLabel.text = viewModel.status?.user?.screen_name
            vipImgView.image = viewModel.vipImage
            timeLabel.text = viewModel.createText
            if let source = viewModel.sourceText {
                sourceLabel.text = "来自" + source
            } else {
                sourceLabel.text = nil
            }
            contentLabel.attributedText = FindEmoticon.shareInstance.findAttributeString(statusText: viewModel.status?.text, font: contentLabel.font)
            contentLabel.textColor = UIColor.black
            contentLabel.matchTextColor = UIColor(red: 86/255.0, green: 156/255.0, blue: 164/255.0, alpha: 1)
            
            //设置昵称的文字颜色
            nickNameLabel.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
            
            //计算picView的宽度和高度的约束
            let picViewSize = calculatePicsWidthAndHeight(count: viewModel.picUrls.count)
            pictureViewW.constant = picViewSize.width
            pictureViewH.constant = picViewSize.height
            
            //将picURL数据传递给picView
            picCollectionView.picUrls = viewModel.picUrls
            
            //设置转发微博的正文
            if viewModel.status?.retweetedstatus != nil {
                //转发微博的正文
                if let screenname = viewModel.status?.retweetedstatus?.user?.screen_name, let text = viewModel.status?.retweetedstatus?.text {
                    retweetedLabel.attributedText = FindEmoticon.shareInstance.findAttributeString(statusText: "@" + screenname + ": " + text, font: retweetedLabel.font)
                    
                    //设置转发正文距离顶部的约束
                    retweetLabelTopconstant.constant = 10;
                    
                    retweetedLabel.textColor = UIColor.black;
                    retweetedLabel.matchTextColor = UIColor(red: 86/255.0, green: 156/255.0, blue: 164/255.0, alpha: 1)
                }
                
                //转发微博的背景
                retweetBgView.isHidden = false
                
            } else {
                //正文
                retweetedLabel.text = nil
                
                //背景
                retweetBgView.isHidden = true
                
                //设置转发正文距离顶部的约束
                retweetLabelTopconstant.constant = 0
            }
            
            //计算cell的高度
            if viewModel.cellHeight == 0 {
                layoutIfNeeded()
                viewModel.cellHeight = toolView.frame.maxY
            }
        }
    }
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置微博正文的宽度约束
        statusWidth.constant = UIScreen.main.bounds.size.width - 2 * edgeMargin
    }
}

// MARK:- 配图的计算方法
extension HomeCell {
    fileprivate func calculatePicsWidthAndHeight(count : Int) -> CGSize {
        //1.没有配图
        if count == 0 {
            picViewBottomConstant.constant = 0
            return CGSize.zero
        }
        
        //有配图需要改约束的值
        picViewBottomConstant.constant = 10
        
        //2.取出picView对应的layout
        let layOut = picCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        //3.设置单张配图的itemSize和collectionview的尺寸
        if count == 1 {
            let urlString = viewModel?.picUrls.last?.absoluteString
            let img = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: urlString)
            guard let image = img else {
                return CGSize.zero
            }
            layOut.itemSize = CGSize(width: image.size.width * 2, height: image.size.height * 2)
            return CGSize(width: layOut.itemSize.width, height: layOut.itemSize.height)
        }
        
        //4.计算出来一张配图的imageViewWH
        let imgWH = (UIScreen.main.bounds.size.width - 2 * edgeMargin - 2 * itemMargin) / 3
        
        //5.其他张配图的itemSize
        layOut.itemSize = CGSize(width: imgWH, height: imgWH)
        
        //6.4张配图
        if count == 4 {
            let picsWH = imgWH * 2 + itemMargin + 1
            return CGSize(width: picsWH, height: picsWH)
        }
        
        //7.其他配图
        //7.1计算行数
        let rows = CGFloat((count - 1) / 3 + 1)
        
        //7.2计算collectionview的宽
        let picW = imgWH * 3 + itemMargin * 2
        
        //7.3计算collectionview的高
        let picH = imgWH * rows + itemMargin * (rows - 1)
        return CGSize(width: picW, height: picH)
    }
}





